package com.mk.service.platform;

import act.controller.Controller;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.*;
import com.mk.domain.CpConfig;
import com.mk.interceptor.ITranslate;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("cpConfig")
@Api(group = "pl", title = "通用配置服务", path = "/cpConfig")
@SuppressWarnings("unused")
public class CpConfigService extends BasePlatformService {
    @Inject
    private CpConfig.Mapper cpConfigMapper;

    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "cpConfig", title = "", type = CpConfig.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public void update(@NotNull CpConfig cpConfig) {
        cpConfigMapper.deleteById(cpConfig.getCfid());
        cpConfigMapper.insert(cpConfig);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "的主键", type = String.class)
    @ApiReturn(description = "", type = CpConfig.class)
    public CpConfig view(@NotNull String id) {
        return cpConfigMapper.single(id);
    }


    @With({ILogin.class, ITranslate.class})
    @GetAction("getConfigIndex.json")
    @ApiMethod(title = "查询配置页相关字典数据", path = "/getConfigIndex.json", method = ApiHttpMethod.GET)
    @ApiReturn(description = "", type = Map.class, maps = {
        @ApiMap(name = "TXSXF", type = String.class, title = "提现手续费"),
        @ApiMap(name = "TXMIN", type = String.class, title = "最小提现金额"),
        @ApiMap(name = "TXMAX", type = String.class, title = "最大提现金额"),
        @ApiMap(name = "CJGBSJ", type = String.class, title = "抽检关闭时间"),
        @ApiMap(name = "ISOPENUSERROLE", type = String.class, title = "是否开启店铺权限"),
            @ApiMap(name = "DISCOUNT", type = String.class, title = "订单折扣"),
            @ApiMap(name = "FSCALE", type = String.class, title = "首单抽佣比例"),
            @ApiMap(name = "RSCALE", type = String.class, title = "返单抽佣比例"),
            @ApiMap(name = "PRESCALE", type = String.class, title = "会长分佣比例")
    })
    public Map<String, Object> getCpconfigIndex() {
        List<CpConfig> list = cpConfigMapper.selectConfigIndex();
        Map<String, Object> map = new HashMap<>();
        list.forEach(item -> map.put(item.getCfid(), item.getCfvalue()));
        return map;
    }

    @With({ILogin.class, ITranslate.class})
    @PutAction("saveConfigIndex.json")
    @ApiMethod(title = "保存配置首页相关配置", path = "/saveConfigIndex.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "trid", title = "记录主键", type = String.class)
    public int saveConfigIndex(List<CpConfig> configs) {
        return cpConfigMapper.saveConfigIndex(configs);
    }

    @With({ILogin.class, ITranslate.class})
    @PutAction("saveConfigList.json")
    @ApiMethod(title = "保存字典列表根据类型", path = "/saveConfigList.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "cftype", title = "记录主键", type = String.class)
    @ApiParam(name = "configs", title = "记录主键", type = List.class, elementType = CpConfig.class)
    public int saveConfigList(String cftype, List<CpConfig> configs) {
        cpConfigMapper.saveConfigList(cftype, configs);
        return 1;
    }

    @With({ILogin.class, ITranslate.class})
    @GetAction("getConfigListByCftype.json")
    @ApiMethod(title = "根据字典类型查询列表", path = "/getConfigListByCftype.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "cftype", title = "记录主键", type = String.class)
    public List<CpConfig> getConfigListByCftype(String cftype) {
        return cpConfigMapper.getConfigListByCftype(cftype);
    }

    @With(ILogin.class)
    @GetAction("getValue.json")
    @ApiMethod(title = "查询指定配置的值", path = "/getValue.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "cfid", title = "cfid", type = String.class)
    @ApiReturn(description = "", type = String.class)
    public String getValue(@NotNull String id) {
        CpConfig config = cpConfigMapper.single(id);
        if (config == null) {
            return "";
        }
        return config.getCfvalue();
    }

}
